package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.SysWechatConfig;
import com.ruoyi.fun.service.ISysWechatConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商户基本信息Controller
 * 
 * @author dreamyy
 * @date 2025-09-09
 */
@RestController
@RequestMapping("/fun/version")
public class SysWechatConfigController extends BaseController
{
    @Autowired
    private ISysWechatConfigService sysWechatConfigService;

    /**
     * 查询商户基本信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysWechatConfig sysWechatConfig)
    {
        // 强制设置orgId为当前用户的部门ID，确保数据隔离
        Long currentDeptId = getLoginUser().getDeptId();
        if (currentDeptId != null) {
            sysWechatConfig.setOrgId(String.valueOf(currentDeptId));
        }
        
        startPage();
        List<SysWechatConfig> list = sysWechatConfigService.selectSysWechatConfigList(sysWechatConfig);
        return getDataTable(list);
    }

    /**
     * 导出商户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('fun:version:export')")
    @Log(title = "商户基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysWechatConfig sysWechatConfig)
    {
        // 强制设置orgId为当前用户的部门ID，确保数据隔离
        Long currentDeptId = getLoginUser().getDeptId();
        if (currentDeptId != null) {
            sysWechatConfig.setOrgId(String.valueOf(currentDeptId));
        }
        
        List<SysWechatConfig> list = sysWechatConfigService.selectSysWechatConfigList(sysWechatConfig);
        ExcelUtil<SysWechatConfig> util = new ExcelUtil<SysWechatConfig>(SysWechatConfig.class);
        util.exportExcel(response, list, "商户基本信息数据");
    }

    /**
     * 获取商户基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:version:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SysWechatConfig config = sysWechatConfigService.selectSysWechatConfigById(id);
        if (config != null) {
            // 验证数据是否属于当前用户的组织
            Long currentDeptId = getLoginUser().getDeptId();
            if (currentDeptId == null || !String.valueOf(currentDeptId).equals(config.getOrgId())) {
                return error("无权限访问该数据");
            }
        }
        return success(config);
    }

    /**
     * 新增商户基本信息
     */
    @PreAuthorize("@ss.hasPermi('fun:version:add')")
    @Log(title = "商户基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysWechatConfig sysWechatConfig)
    {
        // 强制设置orgId为当前用户的部门ID
        Long currentDeptId = getLoginUser().getDeptId();
        if (currentDeptId != null) {
            sysWechatConfig.setOrgId(String.valueOf(currentDeptId));
        }
        return toAjax(sysWechatConfigService.insertSysWechatConfig(sysWechatConfig));
    }

    /**
     * 修改商户基本信息
     */
    @PreAuthorize("@ss.hasPermi('fun:version:edit')")
    @Log(title = "商户基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysWechatConfig sysWechatConfig)
    {
        // 验证数据是否存在且属于当前用户的组织
        SysWechatConfig existingConfig = sysWechatConfigService.selectSysWechatConfigById(sysWechatConfig.getId());
        if (existingConfig == null) {
            return error("数据不存在");
        }
        
        Long currentDeptId = getLoginUser().getDeptId();
        if (currentDeptId == null || !String.valueOf(currentDeptId).equals(existingConfig.getOrgId())) {
            return error("无权限修改该数据");
        }
        
        // 强制设置orgId为当前用户的部门ID，防止越权修改
        sysWechatConfig.setOrgId(String.valueOf(currentDeptId));
        return toAjax(sysWechatConfigService.updateSysWechatConfig(sysWechatConfig));
    }

    /**
     * 删除商户基本信息
     */
    @PreAuthorize("@ss.hasPermi('fun:version:remove')")
    @Log(title = "商户基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysWechatConfigService.deleteSysWechatConfigByIds(ids));
    }
}
